package com.he.controller;

import com.he.annotation.OperationLogger;
import com.he.domain.Result;
import com.he.domain.dto.*;
import com.he.domain.vo.OnlineVO;
import com.he.domain.vo.UserBackInfoVO;
import com.he.domain.vo.UserBackVO;
import com.he.domain.vo.UserRoleVO;
import com.he.service.UserService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author He
 * @version 1.0
 * @Date 2023/10/3 11:01
 * @Desc 用户控制器
 */
@Tag(name = "用户模块")
@RestController
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 获取后台登录用户信息
     *
     * @return {@link UserBackInfoVO} 登录用户信息
     */
    @Operation(description = "获取后台登录用户信息")
    @GetMapping("/admin/user/getUserInfo")
    public Result<UserBackInfoVO> getUserBackInfo() {
        return Result.success(userService.getUserBackInfo());
    }

    /**
     * 修改管理员密码
     *
     * @param password 密码
     * @return {@link Result<>}
     */

    @Operation(description = "修改管理员密码")
    @PreAuthorize("@ps.hasRole('1')")
    @PutMapping("/admin/password")
    public Result<?> updateAdminPassword(@Validated @RequestBody PasswordDTO password) {
        userService.updateAdminPassword(password);
        return Result.success();
    }

    /**
     * 查看后台用户列表
     *
     * @param condition 条件
     * @return {@link UserBackVO} 用户后台列表
     */
    @Operation(description = "查看后台用户列表")
    @PreAuthorize("@ps.hasPermission('system:user:list')")
    @GetMapping("/admin/user/list")
    public Result<PageResult<UserBackVO>> listUserBackVO(ConditionDTO condition) {
        return Result.success(userService.listUserBackVO(condition));
    }

    /**
     * 查看用户角色选项
     *
     * @return {@link UserRoleVO} 用户角色选项
     */
    @Operation(description = "查看用户角色选项")
    @PreAuthorize("@ps.hasPermission('system:user:list')")
    @GetMapping("/admin/user/role")
    public Result<List<UserRoleVO>> listUserRoleDTO() {
        return Result.success(userService.listUserRoleDTO());
    }


    /**
     * 修改用户
     *
     * @param user 用户信息
     * @return {@link Result<>}
     */
    @Operation(description = "修改用户")
    @PreAuthorize("@ps.hasPermission('system:user:update')")
    @PutMapping("/admin/user/update")
    public Result<?> updateUser(@Validated @RequestBody UserRoleDTO user) {
        userService.updateUser(user);
        return Result.success();
    }

    /**
     * 修改用户状态
     *
     * @param disable 禁用信息
     * @return {@link Result<>}
     */
    @Operation(description = "修改用户状态")
    @PreAuthorize("@ps.hasPermission('system:user:update')")
    @PutMapping("/admin/user/changeStatus")
    public Result<?> updateUserStatus(@Validated @RequestBody DisableDTO disable) {
        userService.updateUserStatus(disable);
        return Result.success();
    }

    /**
     * 查看在线用户
     *
     * @param condition 条件
     * @return {@link OnlineVO} 在线用户列表
     */
    @Operation(description = "查看在线用户")
    @PreAuthorize("@ps.hasPermission('monitor:online:list')")
    @GetMapping("/admin/online/list")
    public Result<PageResult<OnlineVO>> listOnlineUser(ConditionDTO condition) {
        return Result.success(userService.listOnlineUser(condition));
    }

    /**
     * 下线用户
     *
     * @param userId
     * @return {@link Result<>}
     */
    @OperationLogger(value = "下线用户")
    @Operation(description = "下线用户")
    @PreAuthorize("@ps.hasPermission('monitor:online:kick')")
    @DeleteMapping("/admin/online/kick/{userId}")
    public Result<?> kickOutUser(@PathVariable("userId") String userId) {
        userService.kickOutUser(userId);
        return Result.success();
    }

}
